package com.teame.readbaby;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.teame.readbaby.BaseActivity.DataCallback;
import com.teame.readbaby.parser.AddressListParser;
import com.teame.readbaby.parser.UserInfoParser;
import com.teame.readbaby.util.ConstantValue;
import com.teame.readbaby.util.GloableParams;
import com.teame.readbaby.util.Logger;
import com.teame.readbaby.vo.AddressListInfo;
import com.teame.readbaby.vo.Userinfo;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends BaseActivity {
	private EditText et_regist_email_edit;
	private EditText et_regist_pwd_edit;
	private EditText et_regist_repwd_edit;
	private TextView tv_register_regist;
	
	
	private String inputEmail; //用户输入的email
	private String inputPassword; //用户输入的密码
	
	
	@Override
	protected void onClickEvent(View paramView) {
		if(paramView==tv_register_regist){
			//处理用户注册
			if(checkInput()){ //用户输入正确
				String url=ConstantValue.HOST+ConstantValue.REGISTER;
				Map<String,String> paramMap=new HashMap<String, String>();
				paramMap.put("username", inputEmail);
				paramMap.put("password", inputPassword);
				this.getDataFromServer(url, paramMap, new UserInfoParser(),new DataCallback<Userinfo>() {

					@Override
					public void processData(Userinfo userInfo, boolean paramBoolean) {
						if (!paramBoolean || userInfo == null) {// 获取失败
							Toast.makeText(context, "用户名已存在", Toast.LENGTH_SHORT).show();
							return ;
						} else {// 获取成功
							GloableParams.userInfo=userInfo;
							Intent intent=new Intent(RegisterActivity.this,AccountManageActivity.class);
							startActivity(intent);
							finish();
						}
					}
					
				});
			}else{  //用户输入错误
				//do nothing
			}
		}
	}
	private boolean checkInput() {
		//检查邮箱
		inputEmail=et_regist_email_edit.getText().toString().trim();
		String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";  
	    Pattern   p   =   Pattern.compile(regex);  
	    Matcher   m   =   p.matcher(inputEmail); 
	    if(!m.matches()){
	    	et_regist_email_edit.setText("");
	    	Toast.makeText(context, "请输入正确的邮箱地址", Toast.LENGTH_SHORT).show();
	    	return false;
	    }
	    //检查密码
	    inputPassword=et_regist_pwd_edit.getText().toString().trim();
	    String inputRePassword=et_regist_repwd_edit.getText().toString().trim();
	    if(inputPassword!=null && inputRePassword!=null && inputPassword.equals(inputRePassword)){
	    	return true;
	    }else{
	    	et_regist_pwd_edit.setText("");
	    	et_regist_repwd_edit.setText("");
	    	Toast.makeText(context, "两次输入的密码必须一致且不能为空", Toast.LENGTH_SHORT).show();
	    	return false;
	    }
	    
	    
	    
	}
	@Override
	protected void findViewById() {
		et_regist_email_edit=(EditText) findViewById(R.id.et_regist_email_edit);
		et_regist_pwd_edit=(EditText) findViewById(R.id.et_regist_pwd_edit);
		et_regist_repwd_edit=(EditText) findViewById(R.id.et_regist_repwd_edit);
		tv_register_regist=(TextView) findViewById(R.id.tv_register_regist);
	}
	@Override
	protected void loadViewLayout() {
		setContentView(R.layout.regist_activity);
	}
	@Override
	protected void processLogic() {
		
	}
	@Override
	protected void setListener() {
		tv_register_regist.setOnClickListener(this);
	}
}
